<template>
	<view class="content">
		<uni-easyinput v-model="name" placeholder="歌单名"></uni-easyinput>
		<button @click="find()">搜索</button>
		<view style="text-align: center;">点击歌单上传</view>
		<uni-list>
			<uni-list :border="true" v-for="(item,index) in list">
				<uni-list-chat 
					clickable 
					:title="item.name" 
					:avatar="item.img" 
					:note="item.about"
					@click="clickMusic(item)"
				>
				</uni-list-chat>
			</uni-list>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'FindCloudMusicList',
				name: '',
				list: []
			}
		},
		onLoad() {

		},
		methods: {
			async find() {
				uni.showLoading({
					title: '搜索中',
					mask:true
				});
				let res = await this.NeteaseCloudMusicTool.findMusicList(this, this.name)
				uni.hideLoading();
				if (res.code === 200) {
					this.list = []
					for (var i = 0; i < res.result.playlists.length; i++) {
						let re = res.result.playlists[i]						
						let albums = {
							"name": re.name,
							"about": re.description,
							"img": re.coverImgUrl,
							"userid": this.Device.deviceId,
							"thirdid": re.id+""
						}
						this.list.push(albums)
					}
				}
			},
			async clickMusic(musicGroup){
				console.log(musicGroup)
				uni.showLoading({
					title: '获取歌单',
					mask:true
				});
				let res = await this.NeteaseCloudMusicTool.findMusicListMsg(this, musicGroup.thirdid)
				uni.hideLoading();
				if (res.code !== 200 ) {
					return
				}
				if(res.songs === undefined || res.songs === null|| res.songs.length === 0){
					return
				}
				let musicList = []
				for (var i = 0; i < res.songs.length; i++) {
					let remusic = res.songs[i]
					let arname = ''
					for (var j = 0; j < remusic.ar.length; j++) {
						arname = arname+remusic.ar[j].name
						if(j !== (remusic.ar.length-1)){
							arname = arname+","
						}
					}
					let music = {
						"name": remusic.name,
						"arname": arname,
						"alname": remusic.al.name,
						"img": remusic.al.picUrl,
						"userid": "1",
						"url": null,
						"thirdid": remusic.id+""
					}
					uni.showLoading({
						title: '获取URL',
						mask:true
					});
					let urlres = await this.NeteaseCloudMusicTool.findMusicUrl(this, music.thirdid)
					uni.hideLoading();
					if (urlres.code !== 200 ) {
						break
					}
					if(urlres.data === undefined || urlres.data === null|| urlres.data.length === 0){
						break
					}
					if(urlres.data[0].url === undefined || urlres.data[0].url === null|| urlres.data[0].url === ''){
						break
					}
					music.url = urlres.data[0].url
					uni.showLoading({
						title: music.name,
						mask:true
					})
					let newmusic = await this.HttpTool.UpMusicByUrl(this, music)
					uni.hideLoading();
					console.log(newmusic);
					if(newmusic.code === 200){
						musicList.push(newmusic.data)
					}
				}
				uni.showLoading({
					title: '创建歌单',
					mask:true
				})
				res = await this.HttpTool.CreateMusicGroup(this, musicGroup)
				uni.hideLoading()
				if(res.code !== 200){
					return
				}
				musicGroup = res.data
				musicGroup.musicList = musicList
				uni.showLoading({
					title: '更新歌单',
					mask:true
				})
				res = await this.HttpTool.updateMusicGroupMusic(this, musicGroup)
				uni.hideLoading()
				if(res.code === 200){
					uni.showToast({
						title: '成功',
						duration: 2000,
					});
					uni.navigateBack()
				}
			}
		}
	}
</script>
<style>
</style>
